package com.qikux.service.impl;

import com.qikux.annotation.Component;
import com.qikux.annotation.Resource;
import com.qikux.annotation.Transactional;
import com.qikux.dao.MessageDao;
import com.qikux.dao.UserDao;
import com.qikux.entity.Notice;
import com.qikux.entity.User;
import com.qikux.service.MessageService;
import com.qikux.vo.Page;
import com.qikux.vo.ResultVo;

import java.util.List;
import java.util.Map;

@Component
@Transactional
public class MessageServiceImpl implements MessageService {
    @Resource
    private MessageDao messageDao;
    @Resource
    private UserDao userDao;
    /**
     * @description
     * @date 2022/9/29 11:29
     * @auther XuHuanDong
     */
    @Override
    public ResultVo<String> queryBlackStatus(Long id, Long resUserId) {
        //查询用户是否关注资源上传者
        List<Map<String, Object>> attentionList = userDao.isAttentionResUserId(id, resUserId);
        if (attentionList.isEmpty()){
            return ResultVo.fail("你未关注对方,无法私信").build();
        }
        //查询用户是否拉黑对方 或被 对方拉黑
        List<Map<String, Object>> list = messageDao.findBlackStatusByUserId(id, resUserId);
        if (!list.isEmpty()){
            for (Map<String, Object> map : list){
                if (map.get("user_id_black") == resUserId){
                    return ResultVo.fail("你已拉黑对方,无法私信").build();
                }
                if (map.get("user_id_black") == id){
                    return ResultVo.fail("你已被对方拉黑,无法私信").build();
                }
            }
        }
        //如果list 为空说明 没有拉黑对方
        return ResultVo.ok().build();
    }



    @Override
    public ResultVo<Page<Map<String, Object>>> queryMsgList(int id, Page<Map<String, Object>> pageBean) {

        List<Map<String,Object>> list = messageDao.findMsgList(id,pageBean);

        //查询用户私信总个数
        int count = messageDao.findMsgCount(id);
        pageBean.setList(list);
        pageBean.setTotalRows(count);
        return ResultVo.ok().body(pageBean);
    }

    @Override
    public ResultVo<Map<String, Object>> queryUserMsg(Long sendid, int reviceid) {
        List<Map<String, Object>> list = messageDao.findUserMsg(sendid, reviceid);

        return ResultVo.ok().body(list);
    }

    @Override
    public ResultVo<Map<String, Object>> blackUser(Long sendid, int bid) {

        int blackCount = messageDao.findBlack(sendid, bid);

        if (blackCount>0){
            return ResultVo.ok().body(blackCount);
        }else{
            int list = messageDao.upblackUser(sendid, bid);
            return ResultVo.ok().body(list);
        }


    }

    @Override
    public ResultVo<Map<String, Object>> querymyBlack(Long sendid, Page<Map<String, Object>> pageBean) {
        List<Map<String, Object>> list = messageDao.findMyBlack(sendid, pageBean);
        //查询拉黑总个数
        int count = messageDao.findMyblackCount(sendid);
        pageBean.setList(list);
        pageBean.setTotalRows(count);
        return ResultVo.ok().body(pageBean);
    }

    /**
     * 移除黑名单
     * @param sendid
     * @param reb
     * @return
     */
    @Override
    public ResultVo<Map<String, Object>> removeBlack(Long sendid, int reb) {
        boolean b;
        int list = messageDao.removeblack(sendid, reb);
        if (list>0){
            b=true;
        }else {
            b=false;
        }
        return ResultVo.ok().body(b);
    }

    /**
     * 未读数量
     * @param sendid
     * @return
     */
    @Override
    public ResultVo<Map<String, Object>> queryunRead(Long sendid) {
        int list = messageDao.findunRead(sendid);

        return ResultVo.ok().body(list);
    }

    /**
     * 已读
     * @param sendid
     * @param upid
     * @return
     */
    @Override
    public ResultVo<Map<String, Object>> upreaded(Long sendid, int upid) {
        boolean b;
        int list = messageDao.upReaded(sendid,upid);
        if (list>0){
            b=true;
        }else{
            b=false;
        }
        return ResultVo.ok().body(b);
    }

    /**
     * 发送信息
     * @param sendid
     * @param recvid
     * @param message
     * @return
     */
    @Override
    public ResultVo<Map<String, Object>> sendMsg(Long sendid, String recvid, String message) {
        boolean b;
        int list = messageDao.sendMsgs(sendid, recvid,message);
        if (list>0){
            b=true;
        }else {
            b=false;
        }
        return ResultVo.ok().body(b);
    }

    /**
     * @description 查询官方通知消息
     * @date 2022/10/10 21:25
     * @auther XuHuanDong
     */
    @Override
    public ResultVo<String> queryAllAnnouncementMsg() {
        List<Map<String, Object>> list = messageDao.findAllAnnouncement();
        return ResultVo.ok().body(list);
    }

    /**
     * @description 查询用户所有通知消息
     * @date 2022/9/29 19:46
     * @auther XuHuanDong
     * @return
     */
    @Override
    public ResultVo<String> queryAllNotifiMsgOfUser(User user) {
        //查询用户所有通知消息
        List<Notice> list = messageDao.findAllNotifiByUserId(user.getId());
        return ResultVo.ok().body(list);
    }
}
